import axios from "axios";
import { Toast } from "vant";

import router from "@/router";
import { Userinfo } from "@/storage";


console.log("-----------------------")
console.log(process.env.NODE_ENV)

var urlroot = "http://hqpos.zoomdo.top";
if (process.env.NODE_ENV == "development") {
    urlroot = "/api";
}


const instance = axios.create({
    baseURL: urlroot,
    timeout: 10000,
});




instance.interceptors.response.use(function (response) {
    if (response.status == 200) {
        var data = response.data || {};
        
        // 返回code 99 是未登录，返回 code98 是无权限
        if (data.code == 99) {
            Userinfo({});
            Toast("登录过期, 请重新登录！");
            router.push("/login")
        } else if (data.code == 98) {
            Toast("无权限！");
            router.push("/login")
        } else {
            return data;
        }
        
    } else {
        Promise.reject(error);
    }
},
    function (error) {
        return Promise.reject(error);
    }
);





// 登录
export const login = (data) => {
    var pars = {
        params: data
    }
    return instance.get("/system/login/login", pars)
}





// 前置配置
export const preConfig  = (data) => {
    var pars = {
        params: data || {}
    }
    return instance.get("/contract/Management/preConfig ", pars)
}


// 审核列表
export const listHY = (data) => {
    var pars = {
        params: data || {}
    }
    return instance.get("/contract/Management/getList", pars)
}


// 合约详情
export const getContractInfo = (data) => {
    var pars = {
        params: data || {}
    }
    return instance.get("/contract/Management/getContractInfo", pars)
}

// 获取承租商信息（乙方）
export const getTenantInfo = (data) => {
    var pars = {
        params: data || {}
    }
    return instance.get("/contract/Management/getTenantInfo", pars)
}


// 获取店铺信息
export const getShopInfo = (data) => {
    data = data || {};
    return instance.post("/contract/Management/getShopInfo", data)
}


// 获取合约租金信息
export const getRents  = (data) => {
    data = data || {};
    return instance.post("/contract/Management/getRents ", data)
}


// 合约审核
export const contractAudit   = (data) => {
    data = data || {};
    return instance.post("/contract/Audit/contractAudit ", data)
}



 

// ============================ 商品 ==============================

// 商品列表
export const getGoodList = (data) => {
    var pars = {
        params: data || {}
    }
    return instance.get("/merchants/Goods/GetList", pars)
}


// 获取单条审核数据
export const getGoodOverview = (data) => {
    var pars = {
        params: data || {}
    }
    return instance.get("/merchants/Goods/View", pars)
}


// 商品审核
export const auditGoods = (data) => {
    var pars = {
        params: data || {}
    }
    return instance.get("/merchants/Goods/Audit", pars)
}






// ============================ 员工 ==============================

// 员工列表
export const getEmployeeList = (data) => {
    var pars = {
        params: data || {}
    }
    return instance.get("/merchants/Manager/GetList", pars)
}


// 员工详情
export const getEmployeeOvervier = (data) => {
    var pars = {
        params: data || {}
    }
    return instance.get("/merchants/Manager/View", pars)
}

// 员工审核
export const auditEmployee = (data) => {
    var pars = {
        params: data || {}
    }
    return instance.get("/merchants/Manager/Audit", pars)
}




// ============================ 承租商 ==============================

// 列表
export const getTenantList = (data) => {
    var pars = {
        params: data || {}
    }
    return instance.get("/merchants/Tenant/GetList", pars)
}

// 详情
export const getTenantOvervier = (data) => {
    var pars = {
        params: data || {}
    }
    return instance.get("/merchants/Tenant/View", pars)
}




// ============================ 商铺 ==============================

// 列表
export const getShopList = (data) => {
    var pars = {
        params: data || {}
    }
    return instance.get("/merchants/Shop/GetList", pars)
}

// 详情
export const getShopOvervier = (data) => {
    var pars = {
        params: data || {}
    }
    return instance.get("/merchants/Shop/View", pars)
}










// 交易历史
export const transHistory = (data) => {
    var pars = {
        params: data
    }
    return instance.get("/orders/query/closed", pars)
}


// 股票信息
export const getSymbolProfile = (data) => {
    return instance.post("/twodog/symbol/getSymbolProfile", data)
}


// 自选列表
export const favorList = (data) => {
    return instance.post("/twodog/favor/list", data)
}


// 添加自选
export const favorAdd = (stock) => {
    var account = selfAccount.get();
    var symbol = stock.symbol;
    if (stock.exchange) {
        symbol += ("|" + stock.exchange)
    }
    var data = {
        "account_id": account.accountId,
        "group_id": 0,
        "symbols": [symbol]
    }
    return instance.post("/twodog/favor/add", data)
}

// 删除自选
export const favorDel = (stock) => {
    var account = selfAccount.get();
    var symbol = stock.symbol;
    if (stock.exchange) {
        symbol += ("|" + stock.exchange)
    }
    var data = {
        "account_id": account.accountId,
        "group_id": 0,
        "symbols": [symbol]
    }
    return instance.post("/twodog/favor/delete", data)
}



// 精选分类
export const getFeaturedSets = () => {
    return instance.post("/twodog/symbol/getFeaturedSets", {})
}



// 列表分类
export const getListedSets = () => {
    return instance.post("/twodog/symbol/getListedSets", {})
}



// 排行榜
export const rankList = (params) => {
    return instance.post("/twodog/rank/list", params)
}

